package com.yzt.loginmodule.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.arouter.RouterAccount;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.constants.PreferenceConstant;
import com.yzt.zhuju.event.main.LoginCloseEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.loginmodule.R;
import com.yzt.p_base.utils.SharedPreferenceUtil;
import com.yzt.p_base.utils.ToastUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 重制手机号
 */
@Route(path = RouterAccount.ACTIVITY_RESET_PHONE)
public class ResetPhoneActivity extends BaseActivity {

    @Autowired
    String mobile;

    /**
     * 1:登录/注册；2：重置密码；
     */
    @Autowired
    int type;

    @Autowired
    boolean isAgree;

    private EditText etInputMobileNo;

    private TextView tvCountryCode;

    private TextView tv_get_code;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_phone;
    }

    @Override
    public void installViews() {

        observerRxBus(LoginCloseEvent.class, event -> finish());

        setFullScreen(true, R.color.transparent);
        tv_get_code = findViewById(R.id.tv_get_code);
        etInputMobileNo = findViewById(R.id.et_input_mobile_no);
        tvCountryCode = findViewById(R.id.tv_country_code);
        findViewById(R.id.tv_get_code).setOnClickListener(this);
        findViewById(R.id.iv_exit).setOnClickListener(this);

        tvCountryCode.setOnClickListener(this);

        if (TextUtils.isEmpty(mobile)) {
            mobile = SharedPreferenceUtil.getInstance().getValue(PreferenceConstant.LOGIN_MOBILE, "");
        }

        if (!TextUtils.isEmpty(mobile)) {
            etInputMobileNo.setText(mobile);
        }
    }

    @Override
    protected void onClick(int clickId) {
        super.onClick(clickId);
        if (clickId == R.id.tv_country_code) {
            //  RouterAccount.RegisterSelectZone(mActivity);
        } else if (clickId == R.id.iv_exit) {
            mActivity.finish();
        } else if (clickId == R.id.tv_get_code) {
            // 获取验证码
            getMsgCode();
        }
    }

    /**
     * 获取验证码
     */
    private void getMsgCode() {
        String mobile = etInputMobileNo.getText().toString();
        if (mobile.length() != 11) {
            ToastUtil.showMessage("请输入完整的手机号");
            return;
        }
        BizLayer.getInstance().getAccountModule().getAccountPhoneUpdateCode(mobile, new AresSubscriber() {
            @Override
            protected void onSuccess(Object o) {
                RouterAccount.VerifyCodeActivity(ResetPhoneActivity.this, mobile, "+86", 3);
                finish();
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                ToastUtil.showMessage(info);
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                closeProgress();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            return;
        }
        if (requestCode == RouterAccount.AccountRequestCode.REQUEST_SELECT_ZONE) {
            setCountryCode(data.getStringExtra("countryNumber"));
        } else {
            setResult(RESULT_OK);
            mActivity.finish();
        }
    }

    private void setCountryCode(String countryNumber) {
        tvCountryCode.setText(countryNumber);
    }

    private Disposable disposable;

    /**
     * 执行倒计时
     */
    @SuppressLint({"StringFormatInvalid", "SetTextI18n"})
    public void startCountCode() {
        final long count = 60;
        disposable = Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .doOnSubscribe(disposable ->
                        tv_get_code.setEnabled(false)
                )
                .subscribe(aLong -> runOnUiThread(() -> {
                    if (count - aLong == 0) {
                        tv_get_code.setText((count - aLong) + "获取验证码");
                        tv_get_code.setEnabled(true);
                    } else {
                        tv_get_code.setText((count - aLong) + "s再次获取");
                    }
                }), throwable -> {

                }, () -> {

                });
    }

    /**
     * 停止定时执行
     */
    protected void stopTimer() {
        if (null != disposable) {
            disposable.dispose();
            disposable = null;
        }
    }

    @Override
    protected void onDestroy() {
        stopTimer();
        super.onDestroy();
    }
}
